# Version: 2.5.1 (stable environment)
FROM registry.baidubce.com/paddlepaddle/paddle:2.5.1

# Workspace
WORKDIR /PaddleOCR

# Clone code from gitee.com(Or download by manual)
# RUN git clone git@gitee.com:paddlepaddle/PaddleOCR.git /PaddleOCR
COPY . /PaddleOCR

# PaddleOCR base on Python3.7 (-i https://mirror.baidu.com/pypi/simple --trusted-host mirror.baidu.com)
RUN pip3 install --upgrade pip \
    && pip3 install -r requirements.txt \
    && pip3 install paddlehub==2.4.0

# Solve conflicts
RUN pip uninstall astroid -y \
    && pip install astroid==2.11.6

# Set models
RUN mkdir -p /PaddleOCR/inference/


# [1] Detect model(Chinese and English)
ARG det_model_ch_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_det_infer.tar
ARG det_model_ch_file=ch_PP-OCRv3_det_infer.tar

ARG det_model_en_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar
ARG det_model_en_file=en_PP-OCRv3_det_infer.tar

#  Multi - lang
# ARG det_model_multi_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/multilingual/Multilingual_PP-OCRv3_det_infer.tar
# ARG det_model_multi_file=Multilingual_PP-OCRv3_det_infer.tar

# [2] Direction classify modle
ARG classify_model_link=https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar
ARG classify_model_file=ch_ppocr_mobile_v2.0_cls_infer.tar

# [3] Recognition model(Chinese and English)
ARG rec_model_ch_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/chinese/ch_PP-OCRv3_rec_infer.tar
ARG rec_model_ch_file=ch_PP-OCRv3_rec_infer.tar

ARG rec_model_en_link=https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar
ARG rec_model_en_file=en_PP-OCRv3_rec_infer.tar


# Download models
# [1] Download orc detect model.
RUN wget --no-check-certificate ${det_model_ch_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${det_model_ch_file} -C /PaddleOCR/inference/

RUN wget --no-check-certificate ${det_model_en_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${det_model_en_file} -C /PaddleOCR/inference/


#RUN wget --no-check-certificate ${det_model_multi_link} -P /PaddleOCR/inference/ \
#    && tar xf /PaddleOCR/inference/${det_model_multi_file} -C /PaddleOCR/inference/

# [2] Download direction classifier.
RUN wget --no-check-certificate ${classify_model_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${classify_model_file} -C /PaddleOCR/inference/

# [3] Download orc recognition model.
RUN wget --no-check-certificate ${rec_model_ch_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${rec_model_ch_file} -C /PaddleOCR/inference/

RUN wget --no-check-certificate ${rec_model_en_link} -P /PaddleOCR/inference/ \
    && tar xf /PaddleOCR/inference/${rec_model_en_file} -C /PaddleOCR/inference/

# Remove invalid *.tar
RUN rm -f /PaddleOCR/inference/*.tar

# Ports
EXPOSE 8866

# Service
CMD ["/bin/bash", "-c", "hub install deploy/hubserving/ocr_system/ && hub serving start -m ocr_system --workers 4"]
